package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;


import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        List<NursingProject> list = nursingProjectMapper.selectByPage(pageNum, pageSize, name, status);
        Page<NursingProject> pages = (Page<NursingProject>) list;
        return PageResponse.of(pages, NursingProjectVo.class);
    }

    @Override
    public void addProject(NursingProjectDto nursingProjectDto) {
        try {
        nursingProjectMapper.addProject(BeanUtil.toBean(nursingProjectDto, NursingProject.class));
        }
        catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("信我,你钱不够");
        }
    }

    @Override
    public NursingProjectVo selectById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    @Override
    public void updateproject(NursingProjectDto nursingProjectDto) {
        try {
        nursingProjectMapper.updateproject(BeanUtil.toBean(nursingProjectDto, NursingProject.class));
        }
        catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("信我,你钱不够");
        }
    }

    @Override
    public void ture_false(Long id, Integer status) {
        nursingProjectMapper.ture_false(id, status);
    }

    @Override
    public void deleteproject(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        if(ObjectUtil.isNotEmpty(nursingProject) && nursingProject.getStatus().equals(1)){
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }
        nursingProjectMapper.deleteById(id);
    }

    @Override
    public List<NursingProjectVo> selectAll() {
        return nursingProjectMapper.selectAll();
    }


}
